<?php

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require_once('includes/lib_template.php');
/*------------------------------------------------------ */
//-- 提交模板内容设置
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'setting')
{
    admin_priv('template_setup');
    $curr_template = $_CFG['template'];
    
    //1更新模块本身的信息
    $db->query("UPDATE " .$ecs->table('template'). " SET region='" . $_POST['region'] . "',sort_order=" .$_POST['sort_order']. " WHERE remarks = '' AND filename = '" . $_POST[template_file] . "' AND theme = '" . $curr_template . "' AND library='" .$_POST[lib] ."'");
	//2更新原区域信息
	
	//3更新新区域信息
    /* 修改模板文件 */
    $template_file    = '../themes/' . $curr_template . '/' . $_POST['template_file'] . '.dwt';
    $template_content = file_get_contents($template_file);
    $template_content = str_replace("\xEF\xBB\xBF", '', $template_content);
    $org_regions      = get_template_region($curr_template, $_POST['template_file'].'.dwt', false);
    $region_content   = '';
    $pattern          = '/(<!--\\s*TemplateBeginEditable\\sname="%s"\\s*-->)(.*?)(<!--\\s*TemplateEndEditable\\s*-->)/s';
    $replacement      = "\\1\n%s\\3";
    //$lib_template     = "<!-- #BeginLibraryItem \"%s\" -->\n%s\n <!-- #EndLibraryItem -->\n";
	$lib_template     = "<!-- #BeginLibraryItem \"%s\" --><!-- #EndLibraryItem -->\n";
    foreach ($org_regions AS $region)
    {
        $region_content = ''; // 获取当前区域内容
        foreach ($post_regions AS $lib)
        {
            if ($lib['region'] == $region)
            {
                if (!file_exists('../themes/' . $curr_template . $lib['library']))
                {
                    continue;
                }
                //库代码不拷贝到模板 Knife.2010.12.11
                //$lib_content     = file_get_contents('../themes/' . $curr_template . $lib['library']);
                //$lib_content     = preg_replace('/<meta\\shttp-equiv=["|\']Content-Type["|\']\\scontent=["|\']text\/html;\\scharset=.*["|\']>/i', '', $lib_content);
                //$lib_content     = str_replace("\xEF\xBB\xBF", '', $lib_content);
                //$region_content .= sprintf($lib_template, $lib['library'], $lib_content);
                $region_content .= sprintf($lib_template, $lib['library']);
            }
        }
        /* 替换原来区域内容 */
        $template_content = preg_replace(sprintf($pattern, $region), sprintf($replacement , $region_content), $template_content);
    }
    if (file_put_contents($template_file, $template_content))
    {
        //clear_tpl_files(false, '.dwt.php'); // 清除对应的编译文件
        clear_cache_files();
        $lnk[] = array('text' => $_LANG['go_back'], 'href'=>'template_portal.php?act=setup&template_file=' .$_POST['template_file']);
        sys_msg($_LANG['setup_success'], 0, $lnk);
    }
    else
    {
        sys_msg(sprintf($_LANG['modify_dwt_failed'], 'themes/' . $curr_template. '/' . $_POST['template_file'] . '.dwt'), 1, null, false);
    }
}
?>